#!/usr/bin/env python3

from mrjob.job import MRJob
from mrjob.step import MRStep


# method 1: write job
class WordCountJob(MRJob):
    def mapper(self, key, value):
        for word in value.split():
            print()
            yield (word, 1)

    def reducer(self, key, values):
        yield (key, sum(values))


# method 2: write job
class WordCountJob2(MRJob):
    def steps(self):
        return [MRStep(mapper=self.map_line,
                       reducer=self.count_words)]

    def map_line(self, key, value):
        for word in value.split():
            yield (word, 1)

    def count_words(self, key, values):
        yield (key, sum(values))


if __name__ == "__main__":
    WordCountJob2.run()

import subprocess
from subprocess import Popen
import sys

# p = Popen(sys.argv[1:], stdout=subprocess.PIPE)
# out, err = p.communicate()
#
# words = {}
# for line in out.decode("utf-8").split("\n"):
#     if not line:
#         continue
#     p = line.split("\t")
#     key, value = p
#     if key not in words:
#         words[key] = []
#
#     words[key].append(value)
#
# for key, value in words.items():
#     print(key, value)

for line in sys.stdin:
    print("=======" + line)

# if __name__ == "__main__":
#     WordCountJob.run()


# python3 word_count.py ./words.txt
